//
//  SSVisitorView.swift
//  Weibo项目
//
//  Created by vn on 16/7/19.
//  Copyright © 2016年 vn. All rights reserved.
//

import UIKit
import SnapKit
class SSVisitorView: UIView {

    
    var closure:(()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //设置背景图片
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        setUpUI()
        
    }
    
    //纯代码需要实现方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARk:初始化内部子空间
    func setUpUI(){
        
       //添加控件
        addSubview(rodimageView)
        addSubview(coverImageView)
        addSubview(houseImageView)
        addSubview(text_lb)
        addSubview(longin_btn)
        addSubview(reg_btn)
        //设置约束
        rodimageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
            
        }
        
        coverImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
            
            
        }
        
        houseImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
            
            
        }
        
        text_lb.snp_makeConstraints { (make) -> Void in
            
            
            make.top.equalTo(coverImageView.snp_bottom).offset(40)
            make.centerX.equalTo(self)
            make.width.equalTo(230)
            make.height.equalTo(36)
            
        }
        
        longin_btn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(text_lb.snp_bottom).offset(20)
            make.leading.equalTo(text_lb)
            make.width.equalTo(100)
            make.height.equalTo(36)
            
        }
        
        
        reg_btn.snp_makeConstraints { (make) -> Void in
            
            
            make.top.equalTo(text_lb.snp_bottom).offset(20)
            make.trailing.equalTo(text_lb)
            make.width.equalTo(100)
            make.height.equalTo(36)

            
        }
        
        
    }
    
        //按钮监听方法
    func loginClick(){
        
        closure?()
        
    }
    
     //给外界调用的方法,用来改变rodImageView和TextLabel的值
     func setupVisitorViewInfo(imageName:String?,title:String?){
        
        //不为空就说明不是首页界面
        if let img = imageName,let title = title{
            
            rodimageView.image = UIImage(named:img)
            text_lb.text = title
            
            //隐藏遮照图片
            coverImageView.hidden = true
            houseImageView.hidden = true
            
        }else{
            
            //走到这里就说明是首页,让图片动画
            
            rodimageViewAnimation()

            
            
        }
        
        
    }
    
    
    
    //圆形图片动画方法
    func rodimageViewAnimation(){
        
       let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        
        //设置toValue
        anim.toValue = M_PI * 2;
        
        //设置动画时间
        anim.duration = 10;
        
        //设置动画滚动次数
        anim.repeatCount = MAXFLOAT
        
        // 当切换控制器或者程序退到后台 默认动画会停止
        anim.removedOnCompletion = false

        //添加动画
        rodimageView.layer .addAnimation(anim, forKey: nil)
        
        
        
    }
    
   
    //MARk: -lazy空间
    //圆形图片
    lazy var rodimageView:UIImageView = UIImageView(imageName:"visitordiscover_feed_image_smallicon")
    
    //遮挡图片
    lazy var coverImageView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    //小房子
    lazy var houseImageView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    
    //textLabel
    lazy var text_lb:UILabel = {
        
        let lab = UILabel()
        
        lab.text = "关注一些人,回这里看看有什么惊喜 关注一些人,回这里看看有什么惊喜"
        lab.textColor = UIColor.grayColor()
        lab.font = UIFont.systemFontOfSize(14)
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        
        return lab
        
    }()
    
    //登录按钮
    lazy var longin_btn:UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn .setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        return btn
        
    }()
    
    //注册按钮
    lazy var reg_btn:UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn .setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
         return btn
        
    }()

    
    
    
    
    
}
